﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using TimKen.DataAccess;
using System.Web.Services;
namespace TimKen.Web.Admin.Advertisement
{
    public partial class BannerManger : TimKen.App.AdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindResources();
                if (Request["Imtype"] != null && Request["Imtype"].ToString() != "")
                {
                    ViewState["Imtype"] = Request["Imtype"].ToString();
                }
              
                bindData();
            }
        }
        public void BindResources()
        {
            //  btnSearch.Text = GetLangString("Search");
            grdOrderList.Columns[3].HeaderText ="URL";
            grdOrderList.Columns[5].HeaderText = "Time";
           
        }
        public void bindData()
        {
            DataSet dt = new AdvertisementDAO().GetAdvertisement(Convert.ToInt32(ViewState["Imtype"]), this.AspNetPager1.PageSize, this.AspNetPager1.CurrentPageIndex);
            if (dt != null && dt.Tables.Count > 0)
            {
                if (dt.Tables[1].Rows.Count > 0)
                {
                    this.AspNetPager1.RecordCount = Convert.ToInt32(dt.Tables[1].Rows[0]["toal"]);
                }
                this.grdOrderList.DataSource = dt.Tables[0];
                this.grdOrderList.DataBind();
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            bindData();
        }

        protected void grdOrderList_Sorting(object sender, GridViewSortEventArgs e)
        {

        }

        protected void grdOrderList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowType == DataControlRowType.DataRow)
            //{
            //    //ScriptManager.GetCurrent(this.Page).RegisterAsyncPostBackControl(e.Row.Cells[5].FindControl("Ord_Up") as ImageButton);
            //    //ScriptManager.GetCurrent(this.Page).RegisterAsyncPostBackControl(e.Row.Cells[5].FindControl("Ord_Down") as ImageButton);
            //    CheckBox ch = e.Row.Cells[0].FindControl("ck_Tb") as CheckBox;
            //    DataRowView row = (DataRowView)e.Row.DataItem;
            //    if (row["isShow"].ToString() == "1")
            //    {
            //        ch.Checked = true;
            //    }
            //}

        }
        [WebMethod]
        public static bool updateOrder(string id1, string id2)
        {
            AdvertisementDAO dao = new AdvertisementDAO();
            return dao.ExecOrder(id1, id2);
        }
        //[WebMethod]
        //public static bool updateZt(string id)
        //{
        //    AdvertisementDAO dao = new AdvertisementDAO();
        //    return dao.updateZt(id);
        //}
        protected void grdOrderList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            AdvertisementDAO dao = new AdvertisementDAO();
            if (e.CommandName == "del")
            {
                bool bp = dao.Delete(e.CommandArgument.ToString());
                if (bp)
                {
                    bindData();
                    ScriptManager.RegisterStartupScript(this.Master.FindControl("mainPanel") as UpdatePanel, this.Page.GetType(), "", "Al();", true);
                }
                else
                {
                    bindData();
                    ScriptManager.RegisterStartupScript(this.Master.FindControl("mainPanel") as UpdatePanel, this.Page.GetType(), "", "Als();", true);
                }

            }
            else if (e.CommandName == "upd")
            {
                Response.Redirect("~/Admin/Advertisement/Addvertisement.aspx?Imtype=" + ViewState["Imtype"].ToString() + "&Cz=update&id=" + e.CommandArgument.ToString());
            }
            else if (e.CommandName == "UpdateOrder")
            {
                ImageButton lb = (ImageButton)e.CommandSource;
                CheckBox t = lb.Parent.Controls[1] as CheckBox;
                if (t.Checked == true)
                {
                    if (dao.getIsShowNum(ViewState["Imtype"].ToString()) < 5)
                    {
                       bool bo=  dao.updateZt("0", e.CommandArgument.ToString());
                       if (bo) {
                           ScriptManager.RegisterStartupScript(this.Master.FindControl("mainPanel") as UpdatePanel, this.Page.GetType(), "", "zt(1);", true); 
                       } else {
                           ScriptManager.RegisterStartupScript(this.Master.FindControl("mainPanel") as UpdatePanel, this.Page.GetType(), "", "zt(0);", true); 
                       }
                    }
                    else {
                        t.Checked = false;
                        ScriptManager.RegisterStartupScript(this.Master.FindControl("mainPanel") as UpdatePanel, this.Page.GetType(), "", "ShowNum();", true); 
                    }
                }
                else {
                  bool bo=   dao.updateZt("1", e.CommandArgument.ToString());
                  if (bo)
                  {
                      ScriptManager.RegisterStartupScript(this.Master.FindControl("mainPanel") as UpdatePanel, this.Page.GetType(), "", "zt(1);", true);
                  }
                  else
                  {
                      ScriptManager.RegisterStartupScript(this.Master.FindControl("mainPanel") as UpdatePanel, this.Page.GetType(), "", "zt(0);", true);
                  }
                }
               
            }
        }
        public string isShow(string i)
        {
            if (i == "0")
            {
                return "false";
            }
            else if (i == "1")
            {
                return "true";
            }
            else
            {
                return "false";
            }

        }
    }
}